﻿using System;
using System.Net;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

using WoChu.CF.Version;
using WoChu.CF.Version.Model;

using WoChu.RfClient.Login;
using WoChu.RfClient.Common;


namespace WoChu.RfClient.Login
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                  if (string.IsNullOrEmpty(this.txtUserCode.Text))
                {
                    MessageBox.Show("用户ID不能为空!");
                    this.txtUserCode.Focus();
                    return;
                }

                // 设置服务器地址
                WoChu.CF.BaseFrame.WebClient.ClientChannelFactory.WEB_URL_ROOT_PATH = "http://" + AppSetting.NewInstance.GetString("Server") + "/{0}.svc";

                // 自动更新
                //string version = AppSetting.NewInstance.GetString("Version");
                //if (version != null && !version.Equals(string.Empty))
                //{
                //    string[] split = version.Split('|');
                //    string versionCode = split[0];
                //    int majorID = Convert.ToInt32(split[1]);
                //    int subID = Convert.ToInt32(split[2]);
                //    int buidNo = Convert.ToInt32(split[3]);
                //    CF.BaseFrame.Util.TraceEx.Write("开始对比版本");
                //    VersionInfo versionInfo = null;
                //    bool needUpdate = UpdateVersion.NewInstance.CheckVersion(majorID, subID, buidNo);
                //    if (needUpdate)
                //    {
                //        CF.BaseFrame.Util.TraceEx.Write("开始获取新版本" + majorID.ToString() + subID.ToString() + buidNo.ToString());
                //        MyProgressClass.InitProgress(this);
                //        MyProgressClass.Show();
                //        MyProgressClass.NextPosition("需要下载新版本...");
                //        try
                //        {
                //            versionInfo = UpdateVersion.NewInstance.GetLastVersion(majorID, subID, buidNo);
                //            CF.BaseFrame.Util.TraceEx.Write("结束获取新版本");
                //        }
                //        catch (Exception ex)
                //        {
                //            WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
                //            return;
                //        }
                //    }

                //    if (versionInfo != null)
                //    {
                //        CF.BaseFrame.Util.TraceEx.Write("修改版本号" + versionInfo.MAJORID.ToString() + versionInfo.SUBVERSIONID.ToString() + versionInfo.BIULDNO.ToString());
                //        AppSetting.NewInstance.SetString("VersionDesc", versionCode + "|" + versionInfo.MAJORID + "|" + versionInfo.SUBVERSIONID + "|" + versionInfo.BIULDNO);

                //        AppSetting.NewInstance.Write();
                //        CF.BaseFrame.Util.TraceEx.Write("结束修改版本，开始下载新版本文件");
                //        AutoUpdate.UpdateNewVersion(versionInfo);
                //        MyProgressClass.EndProgress(this);
                //        CF.BaseFrame.Util.TraceEx.Write("结束新版本文件下载");

                //        return;
                //    }
                //}

                string strPwd = this.txtPwd.Text;
                if (string.IsNullOrEmpty(strPwd)) strPwd = "8";
                var re = LoginUIRule.NewInstance.LogonUser(this.txtUserCode.Text.Trim(), strPwd);
                if (re.ResultCode.Equals("1"))
                {
                    // 记录用户信息
                    AppSetting.NewInstance.SetString("UserID", txtUserCode.Text);
                    AppSetting.NewInstance.Write();

                    LoginBean.Instance.USERID = re.Params.USERID;
                    LoginBean.Instance.USERCODE = re.Params.USERCODE;
                    LoginBean.Instance.PASSWORD = re.Params.PASSWORD;
                    LoginBean.Instance.USERNAME = re.Params.USERNAME;
                    LoginBean.Instance.loginForm = this;

                    // 登录成功
                    FrmMain frm = new FrmMain();
                    frm.Text = frm.Text + "-" + LoginBean.Instance.USERNAME;
                    frm.userPsw = strPwd;
                    frm.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show(re.Message);
                    return;
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
                this.txtUserCode.Focus();
            }
        }

        // 退出登录
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            Application.Exit();
        }

        private void txtUserCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Enter))
            {
                if (string.IsNullOrEmpty(this.txtUserCode.Text))
                {
                    MessageBox.Show("用户ID不能为空!");
                    return;
                }

                this.txtPwd.Text = string.Empty;
                this.txtPwd.Focus();
            }
        }

        private void txtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Enter))
            {
                this.btnLogin_Click(null, null);
            }
        }
        
        private void FrmLogin_Load(object sender, EventArgs e)
        {

            this.txtUserCode.Text = AppSetting.NewInstance.GetString("UserID");

            string _version = AppSetting.NewInstance.GetString("Version"); //System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            string[] split = _version.Split('|');
            string versionCode = split[1] + "." + split[2] + "."+split[3];      

            this.lblVersionDesc.Text = string.Format(lblVersionDesc.Text, AppSetting.NewInstance.GetString("Server")) + "," + versionCode; // string.Format(this.lblVersionDesc.Text, AppSetting.NewInstance.GetString("VersionD"));

            this.label3.Text = AppSetting.NewInstance.GetString("VersionDesc").ToString();
        }

        private void FrmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
                this.Dispose();
                Application.Exit();
            }
        }

        private void FrmLogin_Closed(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}